package com.servlet;

import com.alibaba.fastjson.JSON;
import com.entity.composite.DeveloperInfo;
import com.entity.composite.IntegrationPackageDownloadInfo;
import com.entity.composite.IntegrationPackageInfo;
import com.entity.element_relation.Element;
import com.entity.short_reviews.IntegrationPackageShortReviews;
import com.entity.version_relation.Version;
import com.service.IntegrationPackageService;
import com.service.impl.IntegrationPackageServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 整合包
 * @author wang_
 * @version 2024.3.4.1
 * @description
 * @date 2025/7/14
 */
@WebServlet("/IntegrationPackageServlet")
public class IntegrationPackageServlet extends AbstractHttpServlet {
	private final Logger                    LOG                       = LogManager.getLogger(IntegrationPackageServlet.class);
	
	@Override
	public void doHandle(HttpServletRequest request, HttpServletResponse response, PrintWriter out, HttpSession session) throws IOException, ServletException {
		String integrationPackageId = request.getParameter("integrationPackageId");
		String type = request.getParameter("type");
		switch (type) {
			// 获取精选整合包
			case "getSelectedIntegrationPackages" -> getSelectedIntegrationPackages(out);
			// 获取整合包的基本信息
			case "getBasicInformation" -> getBasicInformation(out, integrationPackageId);
			// 获取整合包开发者
			case "getDeveloper" -> getDeveloper(out, integrationPackageId);
			// 获取整合包元素
			case "getElement" -> getElement(out, integrationPackageId);
			// 获取整合包版本
			case "getVersion" -> getVersion(out, integrationPackageId);
			// 获取整合包短评
			case "getShortReviews" -> getShortReviews(out, integrationPackageId);
			// 获取整合包下载
			case "getDownload" -> getDownload(out, integrationPackageId);
		}
	}
	
	private static void getSelectedIntegrationPackages(PrintWriter out) {
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		List<IntegrationPackageInfo> selectedIntegrationPackages = service.getSelectedIntegrationPackages();
		String json = JSON.toJSONString(selectedIntegrationPackages);
		out.print(json);
	}
	
	private static void getBasicInformation(PrintWriter out, String integrationPackageId) {
		if (integrationPackageId == null) {
			return;
		}
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		IntegrationPackageInfo integrationPackageInfo = service.getBasicInformation(Integer.parseInt(integrationPackageId));
		String json = JSON.toJSONString(integrationPackageInfo);
		out.print(json);
	}
	
	private static void getDeveloper(PrintWriter out, String integrationPackageId) {
		if (integrationPackageId == null) {
			return;
		}
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		List<DeveloperInfo> integrationPackageInfo = service.getDeveloper(Integer.parseInt(integrationPackageId));
		String json = JSON.toJSONString(integrationPackageInfo);
		out.print(json);
	}
	
	private static void getElement(PrintWriter out, String integrationPackageId) {
		if (integrationPackageId == null) {
			return;
		}
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		List<Element> integrationPackageInfo = service.getElement(Integer.parseInt(integrationPackageId));
		String json = JSON.toJSONString(integrationPackageInfo);
		out.print(json);
	}
	
	private static void getVersion(PrintWriter out, String integrationPackageId) {
		if (integrationPackageId == null) {
			return;
		}
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		List<Version> integrationPackageInfo = service.getVersion(Integer.parseInt(integrationPackageId));
		String json = JSON.toJSONString(integrationPackageInfo);
		out.print(json);
	}
	
	private static void getShortReviews(PrintWriter out, String integrationPackageId) {
		if (integrationPackageId == null) {
			return;
		}
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		List<IntegrationPackageShortReviews> integrationPackageInfo = service.getShortReviews(Integer.parseInt(integrationPackageId));
		String json = JSON.toJSONString(integrationPackageInfo);
		out.print(json);
	}
	
	private static void getDownload(PrintWriter out, String integrationPackageId) {
		if (integrationPackageId == null) {
			return;
		}
		IntegrationPackageService service = new IntegrationPackageServiceImpl();
		List<IntegrationPackageDownloadInfo> integrationPackageInfo = service.getDownload(Integer.parseInt(integrationPackageId));
		String json = JSON.toJSONString(integrationPackageInfo);
		out.print(json);
	}
}
